package com.esun.payment_api.controller;

import com.esun.common.api.CommonResult;
import com.esun.common.constant.Constants;
import com.esun.common.utils.*;
import com.esun.payment_api.bean.UserBean;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.security.AjaxResult;
import com.esun.payment_api.security.model.ChangePasswordBody;
import com.esun.payment_api.security.model.LoginBody;
import com.esun.payment_api.security.model.LoginUser;
import com.esun.payment_api.service.LogService;
import com.esun.payment_api.service.UnitService;
import com.esun.payment_api.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController extends BaseController {
    //test

    @Resource
    UserService userService;

    @Resource
    LogService logService;
    final String moduleName = "APEX端";

    @RequestMapping("/index")
    public String index(){
        return "Welcome to ESUN !!!";
    }

    @PostMapping("/loginTest")
    public AjaxResult loginTest(@RequestBody LoginBody loginBody){
        System.out.println("收到请求");
        HashMap<String, Object> map = new HashMap<>();
        map.put("originPwd", loginBody.getPassword());
        map.put("encodePwd", SecurityUtils.encryptPassword(loginBody.getPassword()));
        map.put("ip", IpUtils.getIpAddr(ServletUtils.getRequest()));
        LoginUser loginUser = getLoginUser();
        map.put("userCoID", loginUser.getCoId());
        return AjaxResult.success("success", map);
    }

    @GetMapping("/tokenGenerator")
    public CommonResult tokenGenerator(Short userId, Short deptId){
        Map map = new HashMap();
        User user = new User();
        user.setId((short)167);
        user.setCoId((short)22);
        user.setDeptId((short)84);
        if(!BlankUtils.isBlank(userId)){
            user.setId(userId);
        }
        if(!BlankUtils.isBlank(deptId)){
            user.setDeptId(deptId);
        }
        map.put("id", user.getId().toString());
        map.put("uuid", user.getUuid());
        map.put("loginName", user.getLoginName());
        map.put("userName", user.getUserName());
        map.put("roleStr", user.getRoleStr());
//        map.put("departmentName", "信息中心");

        if(BlankUtils.isBlank(user.getCoId())){
            map.put("coId", null);
        }else{
            map.put("coId", user.getCoId().toString());
        }
        if(BlankUtils.isBlank(user.getDeptId())){
            map.put("deptId", null);
        }else{
            map.put("deptId", user.getDeptId().toString());
        }

        return CommonResult.success(Constants.TOKEN_PREFIX + JWTUtils.createToken(map));
    }

    @RequestMapping("/decodeToken")
    public void decodeToken(){
        try{
            User user = getLoginUser();
            System.out.println(user);
        }catch (Exception e){
            System.out.println("无法获取用户");
        }
    }

    //为Apex端提供的修改密码（带Aes加密）
    @PostMapping("/changePassword")
    public CommonResult changePassword(@RequestBody ChangePasswordBody changePasswordBody){
        if(changePasswordBody.getId()==null||changePasswordBody.getOldPassword()==null||changePasswordBody.getNewPassword()==null){
            return CommonResult.failed("用户id、旧密码或新密码没有收到");
        }
        CommonResult commonResult = userService.changePassword(changePasswordBody);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //为Apex端提供的修改密码（不带Aes加密）
    @PostMapping("/changePasswordRaw")
    public CommonResult changePasswordRaw(@RequestBody ChangePasswordBody changePasswordBody){
        if(changePasswordBody.getId()==null||changePasswordBody.getOldPassword()==null||changePasswordBody.getNewPassword()==null){
            return CommonResult.failed("用户id、旧密码或新密码没有收到");
        }
        CommonResult commonResult = userService.changePasswordRaw(changePasswordBody);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

}
